package com.aaa.controller.system;

import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.util.ObjectUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.LayUiTree;
import com.aaa.entity.Result;
import com.aaa.entity.system.Menu;
import com.aaa.service.system.MenuService;
import com.aaa.utils.TreeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2022-08-29 15:57:16
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;


    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("queryAllMenu.do")
    public Result queryAllMenu() {
        List<Menu> menus = menuService.queryAllMenu();
        return success(menus);
    }

    /**
     * 树形
     *
     * @return
     */
    @GetMapping("queryAllMenus.do")
    public List queryAllMenus() {
        List<Menu> menus = menuService.queryAllMenu();
        List<LayUiTree> layUiTrees = TreeUtil.fromMenuListToTreeList(menus);
        return layUiTrees;
    }

    /**
     * 删除(逻辑删除)
     *
     * @param menuId
     * @return
     */
    @PostMapping("deleteMenuById.do")
    public Result deleteMenuById(Integer menuId) {
        boolean b = menuService.deleteById(menuId);
        return success(b);
    }

    /**
     * 跳转页面
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("toShowMenu.do")
    public ModelAndView toShowMenu(ModelAndView modelAndView) {
        modelAndView.setViewName("system/showMenu");
        return modelAndView;
    }

    /**
     * 添加
     *
     * @param menu
     * @return
     */
    @PostMapping("insertMenu.do")
    public Result insertMenu(Menu menu) {
        if (ObjectUtil.isEmpty(menu.getParentId())) {
            menu.setParentId(0);
        }
        Menu insert = menuService.insert(menu);
        return success(insert);
    }

    /**
     * 修改
     *
     * @param menu
     * @return
     */
    @PostMapping("updateMenu.do")
    public Result updateMenu(Menu menu) {
        try {
            Menu update = menuService.update(menu);
            return success(update);
        }catch (Exception e){
            return fail();
        }
    }

    @GetMapping("findParentNameId")
    public Result findParentNameId(Integer id){
        Menu parentNameId = menuService.findParentNameId(id);
        return success(parentNameId);
    }


}

